<?php
namespace app\home\controller;

use app\common\component\user\UserService;
use app\common\logic\UserWalletGold as UserWalletGoldLogic;
use app\common\component\user_wallet_gold\UserWalletGoldModel;

class UserWalletGold extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
		
		//(new Appointment()) -> checkFirstOrder();
	}

	public function index() {
		$currentUserWalletGold = (new UserWalletGoldLogic()) -> getInterest();
		$this -> view -> assign('currentUserWalletGold', $currentUserWalletGold['data']['user_wallet_gold']);

		$this -> view -> assign('userWalletGolds', UserWalletGoldModel::where('user_id', \think\Session::get('user_id')) -> order('status ASC') -> paginate(10, true));

		return $this -> view -> fetch();
	}

	public function topUp() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			if($post['price'] > getParam('WALLET_GOLD_TOP_UP_MAX')) {
				echo '<meta charset="utf-8"><script>alert("您充值金额大于指定金额");window.history.go(-1);</script>';exit;
			}

			if($post['price'] < getParam('WALLET_GOLD_TOP_UP_MIN')) {
				echo '<meta charset="utf-8"><script>alert("您充值金额小于指定金额");window.history.go(-1);</script>';exit;
			}
			
			if(UserWalletGoldModel::where('status in(1,2,3,4) AND user_id = ' . \think\Session::get('user_id')) -> find()) {
				echo '<meta charset="utf-8"><script>alert("您本次未结束，请结束后再充值！");window.location.href="/wallet/gold";</script>';exit;
			}
			$post['pic'] = uploadFile();

			$resultUserWalletGold = (new UserWalletGoldLogic()) -> createV01($post);
			if($resultUserWalletGold['code'] !== 0) {
				echo '<meta charset="utf-8"><script>alert("' . $resultUserWalletGold['message'] . '!");window.history.go(-1);</script>';exit;
			}
			echo '<meta charset="utf-8"><script>alert("' . $resultUserWalletGold['message'] . '!");window.location.href="/wallet/gold";</script>';exit;
		}

		return $this -> view -> fetch();
	}

	public function withdraw() {
		$resultUserWalletGold = UserWalletGoldModel::where('status = 4 AND user_id = ' . \think\Session::get('user_id')) -> find();

		if($resultUserWalletGold) {
			$price = $resultUserWalletGold -> price + $resultUserWalletGold -> interest;
		} else {
			$price = 0;
		}

		if($this -> request -> isPost()) {
			if($price == 0) {
				echo '<meta charset="utf-8"><script>alert("您没有可提现金豆!");window.history.go(-1);</script>';
			} else {
				$post = $this -> request -> post();

				$resultCheckPayPassword = (new UserService()) -> checkPayPassword($post['pay_password']);
		    	if($resultCheckPayPassword['code'] !== 0) {
		    		echo '<meta charset="utf-8"><script>alert("您输入的支付密码有误!");window.history.go(-1);</script>';exit;
		    	}

				$resultUserWalletGold -> status = 2;

				if($resultUserWalletGold -> save()) {
					echo '<meta charset="utf-8"><script>alert("提现成功!");window.location.href="/wallet/gold";</script>';exit;
				}
				echo '<meta charset="utf-8"><script>alert("提现失败!");window.history.go(-1);</script>';exit;
			}
		}

		$resultUser = (new UserService) -> getUser();
    	$this -> view -> assign('user', $resultUser['data']['user']);

		$this -> view -> assign('price', $price);
		
		return $this -> view -> fetch();
	}
}